<?php
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Wellcome to Charity Organization!</title>
</head>

<body>
<div id="container">
		<div id="header">
			<h1>Charity<span class="off">Organization</span></h1>
        </div>   
        <div id="menu">
        </div>
    
		<div id="content">
        <div id="content_top"></div>
		<div id="content_main">
		<?php
		$link =mysql_connect('localhost','root','');
		if (!$link) {
			die('Not connected : ' . mysql_error());
		}
		$db_selected = mysql_select_db('seproject', $link);
		if (!$db_selected) {
			die ('Cannot useSeproject : ' . mysql_error());
		}
		$help=0;
		$query=mysql_query("SELECT Username FROM user");
		for($i=0;$i<mysql_num_rows($query);$i++)
		{
			$a=mysql_fetch_row($query);
			if ($_REQUEST["Username"]==$a[0])
			{
				echo "The username exists! try another one";
				$help=1;
				?>
				<a href="form.php">Back</a>	
				<?php
			}
		}
		if($help==0)
		{
			if($_POST['Password']!=$_POST['retypePassword'])
			{
				echo "Password doesn't match! Retype Again!";
				?>
				<a href="form.php">Back</a>	
				<?php
			}
			else if(strpbrk($_POST['Email'],"@")==false)
			{
				echo "Email is not valid! Retype Again!";
				?>
				<a href="form.php">Back</a>	
				<?php
			}
			else
			{
				$Username= $_POST["Username"]; $FirstName=$_POST["FirstName"];  $LastName=$_POST["LastName"]; $UserType=$_POST["UserType"];$Email=$_POST["Email"]; $Password=$_POST["Password"]; $CellPhone_Number=$_POST["CellPhone_Number"]; $Address=$_POST["Address"];
				$sql=mysql_query("INSERT INTO `seproject`.`user` (`Username`, `FirstName`, `LastName`,`UserType`, `Email`, `Password`, `CellPhone_Number`, `Address`) VALUES ('$Username', '$FirstName', '$LastName', '$UserType','$Email', '$Password', '$CellPhone_Number', '$Address')");
				echo "Congradulations! Your Account has been created successfully!";
				$_SESSION["username"]=$Username;
				?>
				<a href="home.php">Go to homepage</a>	
				<?php
			}
		}
		?>
        </div>
        <div id="content_bottom"></div>
      </div>
   </div>
</body>
</html>
